package org.example.listener;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

import java.io.IOException;

/**
 * pdf页面自定义监听器
 */
public class PdfPageEventListener extends PdfPageEventHelper {
    /**
     * pdf标题字体大小
     */
    private int headerFontSize;

    private int footFontSize;
    /**
     * pdf标题宽度
     */
    private Float headerWidth;
    /**
     * pdf标题高度
     */
    private Float headerHeight;
    /**
     * pdf标题模板
     */
    private PdfTemplate headerTemplate;
    /**
     * pdf标题基础字体
     */
    private BaseFont headerBaseFont;
    /**
     * pdf标题字体样式
     */
    private Font headerFont;

    /**
     * pdf标题字体样式
     */
    private Font footFont;


    private String headerText;


    /**
     * 构建器，进行基本信息初始化
     *
     * @param headerFontSize pdf标题字体大小
     * @param headerWidth    pdf标题宽度
     * @param headerHeight   pdf标题高度
     * @param headerBaseFont pdf标题基础字体
     * @param headerFont     pdf标题字体样式
     */
    public PdfPageEventListener(int headerFontSize, int footFontSize, Float headerWidth, Float headerHeight, BaseFont headerBaseFont, Font headerFont, Font footFont,String headerText) {
        this.headerFontSize = headerFontSize;
        this.footFontSize = footFontSize;
        this.headerWidth = headerWidth;
        this.headerHeight = headerHeight;
        this.headerBaseFont = headerBaseFont;
        this.headerFont = headerFont;
        this.footFont = footFont;
        this.headerText = headerText;
    }

    /**
     * pdf监视器构建
     *
     * @return Builder
     */
    public static Builder build() {
        return new Builder();
    }

    /**
     * 重写文档初始化的模板
     */
    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        headerTemplate = writer.getDirectContent().createTemplate(headerWidth, headerHeight);
    }

    /**
     * 重写每一页结束事件，等所有内容加载完毕后，进行页眉的数据加载和赋值
     */
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            if (headerBaseFont == null) {
                headerBaseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", Boolean.FALSE);
            }
            if (headerFont == null) {
                headerFont = new Font(headerBaseFont, headerFontSize, Font.NORMAL);
            }
            if (footFont == null) {
                footFont = new Font(headerBaseFont, footFontSize, Font.NORMAL);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取文档内容
        PdfContentByte pdfContentByte = writer.getDirectContent();
        float left = document.left();
        float right = document.right();
        float top = document.top();
        float bottom = document.bottom();
        int pageNumber = writer.getPageNumber();
        String previousHeaderText =   pageNumber + " /";
        Phrase headerPhrase = new Phrase(previousHeaderText, footFont);

        Phrase signaturePhrase = new Phrase(headerText, headerFont);
        float headerLen = headerBaseFont.getWidthPoint(previousHeaderText, footFontSize);
        float headerTextLen = headerBaseFont.getWidthPoint(headerText, headerFontSize);
        float logoX = left ;
        float textX = right - headerTextLen / 2 ;
        float footX = right/2;
        float logoY = top + 20F;
        float textY = top + 30F;
        float footY = bottom - 40F;

        try {
         // 页眉
            Image image = Image.getInstance("D:\\test\\logoTest.png");
           /* 从OSS获取文件，构建image
             byte[] imgByte = ossFeign.downloadFile("url");
             Image image1  = Image.getInstance(imgByte);*/
            // 重设图片大小
            image.scaleAbsolute(90, 30);
            image.setAlignment(Image.ALIGN_TOP);
            float[] matrix = image.matrix();
            matrix[Image.CX] = image.getAbsoluteX() - matrix[Image.CX];
            matrix[Image.CY] = image.getAbsoluteY() - matrix[Image.CY];
            pdfContentByte.addImage(image,matrix[0], matrix[1], matrix[2], matrix[3],logoX,logoY);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_CENTER, signaturePhrase, textX, textY, Element.HEADER);
        // 页脚
        ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_CENTER, headerPhrase, footX, footY, Element.HEADER);
        pdfContentByte.addTemplate(headerTemplate, footX + headerLen, footY);

        float lineY = top + 6f;

        CMYKColor magentaColor = new CMYKColor(1.f, 1.f, 1.f, 1.f);
        pdfContentByte.setColorStroke(magentaColor);
        pdfContentByte.moveTo(left, lineY);
        pdfContentByte.lineTo(left, lineY);
        pdfContentByte.closePathStroke();
    }

    /**
     * 文档结束后，将总页码放在文档上
     */
    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        headerTemplate.beginText();
        headerTemplate.setFontAndSize(headerBaseFont, footFontSize);
        int pageNumber = writer.getPageNumber() - 1;
        String behindHeaderText = " " + pageNumber ;
        headerTemplate.showText(behindHeaderText);
        headerTemplate.endText();
        headerTemplate.closePath();
    }

    /**
     * 自定义一个构建器，设置页面相关信息
     */
    public static class Builder {

        private int headerFontSize;
        private int footFontSize;
        private Float headerWidth;
        private Float headerHeight;
        private PdfTemplate headerTemplate;
        private BaseFont headerBaseFont;
        private Font headerFont;
        private Font footFont;
        private String headerText;

        Builder() {
            headerFontSize = 8;
            headerWidth = 50F;
            headerHeight = 50F;
            footFontSize = 10;
            try {
                headerBaseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", Boolean.FALSE);
            } catch (IOException e) {
                e.printStackTrace();
            }
            headerFont = new Font(headerBaseFont, headerFontSize, Font.NORMAL);
            footFont = new Font(headerBaseFont,footFontSize,Font.NORMAL);
            headerText = "";
        }

        public Builder setHeaderFontSize(int headerFontSize) {
            this.headerFontSize = headerFontSize;
            return this;
        }

        public Builder setHeaderWidth(Float headerWidth) {
            this.headerWidth = headerWidth;
            return this;

        }

        public Builder setHeaderHeight(Float headerHeight) {
            this.headerHeight = headerHeight;
            return this;
        }

        public Builder setHeaderTemplate(PdfTemplate headerTemplate) {
            this.headerTemplate = headerTemplate;
            return this;
        }

        public Builder setHeaderBaseFont(BaseFont headerBaseFont) {
            this.headerBaseFont = headerBaseFont;
            return this;
        }

        public Builder setHeaderFont(Font headerFont) {
            this.headerFont = headerFont;
            return this;
        }

        public Builder setHeaderText(String headerText){
            this.headerText = headerText;
            return this;
        }

        public PdfPageEventListener build() {
            return new PdfPageEventListener(headerFontSize,footFontSize, headerWidth, headerHeight, headerBaseFont, headerFont,footFont,headerText);
        }

    }
}
